<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-select v-model="filters.uid" placeholder="请选择">
						<el-option v-for="item in users" :key="item.uid" :label="item.uid" :value="item.uid">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getDevices">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table ref="singleTable" :data="devices" border highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="device_no" label="设备编号" width="140">
			</el-table-column>
			<el-table-column prop="device_type" label="类型" width="100" :formatter="formatType">
			</el-table-column>
			<el-table-column prop="uid" label="所属用户" width="140">
			</el-table-column>
			<el-table-column prop="status" label="状态" width="100" :formatter="formatStatus">
			</el-table-column>
			<el-table-column prop="address" label="安装地址" min-width="220">
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
				<el-form-item label="设备编号" prop="device_no">
					<el-input v-model="editForm.device_no" auto-complete="off" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item label="设备状态" prop="status">
					<el-select v-model="editForm.status" placeholder="请选择">
						<el-option v-for="item in statusEmu" :key="item.key" :label="item.value" :value="item.key">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="所属用户" prop="uid">
					<el-select v-model="editForm.uid" placeholder="请选择">
						<el-option v-for="item in users" :key="item.uid" :label="item.uid" :value="item.uid">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="安装地址" prop="address">
					<el-input v-model="editForm.address"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm2.5上限" prop="critical_value1">
					<el-input v-model="editForm.critical_value1"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm2.5下限" prop="critical_value2">
					<el-input v-model="editForm.critical_value2"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm10上限" prop="critical_value3">
					<el-input v-model="editForm.critical_value3"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm10下限" prop="critical_value4">
					<el-input v-model="editForm.critical_value4"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm100上限" prop="critical_value5">
					<el-input v-model="editForm.critical_value5"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm100下限" prop="critical_value6">
					<el-input v-model="editForm.critical_value6"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
				<el-form-item label="设备编号" prop="device_no">
					<el-input v-model="addForm.device_no" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="类型" prop="device_type">
					<el-select v-model="addForm.device_type" placeholder="请选择">
						<el-option v-for="item in typeEmu" :key="item.key" :label="item.value" :value="item.key">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="所属用户" prop="uid">
					<el-select v-model="addForm.uid" placeholder="请选择">
						<el-option v-for="item in users" :key="item.uid" :label="item.uid" :value="item.uid">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="安装地址" prop="address">
					<el-input v-model="addForm.address"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="设备状态" prop="status">
					<el-input value="正常" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm2.5上限" prop="critical_value1">
					<el-input v-model="addForm.critical_value1"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm2.5下限" prop="critical_value2">
					<el-input v-model="addForm.critical_value2"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm10上限" prop="critical_value3">
					<el-input v-model="addForm.critical_value3"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm10下限" prop="critical_value4">
					<el-input v-model="addForm.critical_value4"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm100上限" prop="critical_value5">
					<el-input v-model="addForm.critical_value5"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm100下限" prop="critical_value6">
					<el-input v-model="addForm.critical_value6"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
export default {
	data() {
		return {
			filters: {
				uid: ''
			},
			devices: [],
			total: 0,
			page: 1,
			listLoading: false,
			sels: [],//列表选中列

			//编辑界面
			editFormVisible: false,
			editLoading: false,
			editFormRules: {
				device_type: [
					{ required: true, message: '请输入设备类型', trigger: 'blur' }
				],
				uid: [
					{ required: true, message: '请选择所属用户', trigger: 'blur' }
				],
				status: [
					{ required: true, message: '请选择设备状态', trigger: 'blur' }
				],
				address: [
					{ required: true, message: '请输入安装地址', trigger: 'blur' }
				],
				critical_value1: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value2: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value3: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value4: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value5: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value6: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				]
			},
			editForm: {
				device_no: '',
				status: '01',
				uid: '',
				address: '',
				critical_value1: null,
				critical_value2: null,
				critical_value3: null,
				critical_value4: null,
				critical_value5: null,
				critical_value6: null
			},

			//新增界面
			addFormVisible: false,
			addLoading: false,
			addFormRules: {
				device_no: [
					{ required: true, message: '请输入设备编号', trigger: 'blur' }
				],
				device_type: [
					{ required: true, message: '请选择设备类型', trigger: 'blur' }
				],
				uid: [
					{ required: true, message: '请选择所属用户', trigger: 'blur' }
				],
				address: [
					{ required: true, message: '请输入安装地址', trigger: 'blur' }
				],
				critical_value1: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value2: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value3: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value4: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value5: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value6: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				]
			},
			addForm: {
				device_no: '',
				device_type: '01',
				uid: '',
				address: '',
				critical_value1: null,
				critical_value2: null,
				critical_value3: null,
				critical_value4: null,
				critical_value5: null,
				critical_value6: null
			},
			statusEmu: [
				{
					key: '00',
					value: '禁用'
				}, {
					key: '01',
					value: '正常'
				}, {
					key: '02',
					value: '故障'
				}
			],
			typeEmu: [
				{
					key: '01',
					value: '监控'
				}, {
					key: '02',
					value: '其他'
				}
			]
		}
	},
	methods: {
		formatType: function(row, column) {
			for (let i = 0; i < this.typeEmu.length; i++) {
				if (row.device_type == this.typeEmu[i].key) {
					return this.typeEmu[i].value
				}
			}
			return '未知类型'
		},
		formatStatus: function(row, column) {
			for (let i = 0; i < this.statusEmu.length; i++) {
				if (row.status == this.statusEmu[i].key) {
					return this.statusEmu[i].value
				}
			}
			return '未知类型'
		},
		handleCurrentChange: function(val) {
			this.page = val
			this.getDevices()
		},
		//获取设备列表
		getDevices: function() {
			this.listLoading = true

			let token = sessionStorage.getItem('token')
			let params = this.formatHttpGetParams({ page: this.page, size: 10, token: token })
			let path = 'api/devices/list/' + this.filters.uid
			let _this = this
			this.$http.get(this.$store.state.domain + path + params).then(function(res) {
				if (res.body.respCode == '0000') {
					this.total = res.body.count
					this.devices = res.body.data
				} else {
					if (res.body.respMsg != null) {
						alert(res.body.respMsg)
					} else {
						alert('后台未知异常，请与管理员联系')
					}
				}
				this.listLoading = false
			}, function() {
				alert('网络请求处理失败')
				this.listLoading = false
			})
		},
		//删除
		handleDel: function(index, row) {
			this.$confirm('确认删除该记录吗?', '提示', {
				type: 'warning'
			}).then(() => {
				this.listLoading = true

				let data = new Object()
				data.device_no = row.device_no

				let token = sessionStorage.getItem('token')
				let params = this.formatHttpGetParams({ token: token })
				let path = 'api/devices/delete'
				this.$http.post(this.$store.state.domain + path + params, JSON.stringify(data), { emulateJSON: true })
					.then(function(res) {
						if (res.body.respCode == '0000') {
							alert('删除成功！')
							this.getDevices()
						} else {
							if (res.body.respMsg != null) {
								alert(res.body.respMsg)
							} else {
								alert('后台未知异常，请与管理员联系')
							}
						}
						this.listLoading = false
					},
					function(error) {
						alert('网络请求处理失败')
						this.listLoading = false
					})
			})
		},
		//显示编辑界面
		handleEdit: function(index, row) {
			this.editFormVisible = true
			this.editForm = Object.assign({}, row)
		},
		//显示新增界面
		handleAdd: function() {
			this.addFormVisible = true
			this.addForm = {
				device_no: '',
				device_type: '01',
				uid: '',
				address: '',
				critical_value1: null,
				critical_value2: null,
				critical_value3: null,
				critical_value4: null,
				critical_value5: null,
				critical_value6: null
			}
		},
		//编辑
		editSubmit: function() {
			this.$refs.editForm.validate((valid) => {
				if (valid) {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.editLoading = true

						let data = new Object()
						data.device_no = this.editForm.device_no
						data.status = this.editForm.status
						data.uid = this.editForm.uid
						data.address = this.editForm.address
						data.critical_value1 = parseFloat(this.editForm.critical_value1)
						data.critical_value2 = parseFloat(this.editForm.critical_value2)
						data.critical_value3 = parseFloat(this.editForm.critical_value3)
						data.critical_value4 = parseFloat(this.editForm.critical_value4)
						data.critical_value5 = parseFloat(this.editForm.critical_value5)
						data.critical_value6 = parseFloat(this.editForm.critical_value6)

						let token = sessionStorage.getItem('token')
						let params = this.formatHttpGetParams({ token: token })
						let path = 'api/devices/update'
						this.$http.post(this.$store.state.domain + path + params, JSON.stringify(data), { emulateJSON: true })
							.then(function(res) {
								if (res.body.respCode == '0000') {
									alert('修改成功！')
									this.$refs['editForm'].resetFields()
									this.editFormVisible = false
									this.editLoading = false
									this.getDevices()
								} else {
									if (res.body.respMsg != null) {
										alert(res.body.respMsg)
									} else {
										alert('后台未知异常，请与管理员联系')
									}
									this.$refs['editForm'].resetFields()
									this.editLoading = false
								}
							},
							function(error) {
								alert('网络请求处理失败')
								this.$refs['editForm'].resetFields()
								this.editLoading = false
							})
					})
				}
			})
		},
		//新增
		addSubmit: function() {
			this.$refs.addForm.validate((valid) => {
				if (valid) {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.addLoading = true

						let data = new Object()
						data.device_no = this.addForm.device_no
						data.device_type = this.addForm.device_type
						data.uid = this.addForm.uid
						data.address = this.addForm.address
						data.critical_value1 = parseFloat(this.addForm.critical_value1)
						data.critical_value2 = parseFloat(this.addForm.critical_value2)
						data.critical_value3 = parseFloat(this.addForm.critical_value3)
						data.critical_value4 = parseFloat(this.addForm.critical_value4)
						data.critical_value5 = parseFloat(this.addForm.critical_value5)
						data.critical_value6 = parseFloat(this.addForm.critical_value6)

						let token = sessionStorage.getItem('token')
						let params = this.formatHttpGetParams({ token: token })
						let path = 'api/devices/add'
						this.$http.post(this.$store.state.domain + path + params, JSON.stringify(data), { emulateJSON: true })
							.then(function(res) {
								if (res.body.respCode == '0000') {
									alert('添加成功！')
									this.$refs['addForm'].resetFields()
									this.addFormVisible = false
									this.addLoading = false
									this.getDevices()
								} else {
									if (res.body.respMsg != null) {
										alert(res.body.respMsg)
									} else {
										alert('后台未知异常，请与管理员联系')
									}
									this.addLoading = false
								}
							},
							function(error) {
								alert('网络请求处理失败')
								this.addLoading = false
							})
					})
				}
			})
		},
		selsChange: function(sels) {
			this.sels = sels
		},
		//批量删除
		batchRemove: function() {
			var ids = this.sels.map(item => item.id).toString()
			this.$confirm('确认删除选中记录吗？', '提示', {
				type: 'warning'
			}).then(() => {
				this.listLoading = true
				let para = { ids: ids }

				//删除操作

				this.listLoading = false
			})
		}
	},
	mounted() {
		var user = sessionStorage.getItem('user')
		if (user) {
			user = JSON.parse(user)
			this.filters.uid = user.uid
		}
		this.getDevices()
	},
	computed: {
		users() {
			return this.$store.state.users
		}
	}
}
</script>

<style scoped>

</style>